package com.wm.reggie.service.impl;/**
 * @author zqx
 * @since 2024/2/2
 */

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wm.reggie.common.CustomException;
import com.wm.reggie.dto.DishDto;
import com.wm.reggie.entity.Dish;
import com.wm.reggie.entity.DishFlavor;
import com.wm.reggie.entity.Setmeal;
import com.wm.reggie.mapper.DishMapper;
import com.wm.reggie.service.CategoryService;
import com.wm.reggie.service.DishFlavorService;
import com.wm.reggie.service.DishService;
import com.wm.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zqx
 * @since
 */
@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品，同时保存口味的数据
     *
     * @param dishDto
     */
    @Transactional
    //同时操作两张表，需要开启事务
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        //获取到菜品id
        Long dishid = dishDto.getId();

        //给dish_flavor表赋上dish_id
        List<DishFlavor> dishFlavorList = dishDto.getFlavors();
        dishFlavorList = dishFlavorList.stream().map((item) -> {
            item.setDishId(dishid);
            return item;
        }).collect(Collectors.toList());

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(dishFlavorList);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {

        //查询菜品基本信息，从dish表查询
        Dish dish = this.getById(id);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        //创建构造条件
        LambdaQueryWrapper<DishFlavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lambdaQueryWrapper);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 修改菜品信息和口味信息
     *
     * @param dishDto
     */
    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //更新dish表
        this.updateById(dishDto);

        //更新dish_flavor表 1.对dish_flavor表的删除
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());

        dishFlavorService.remove(queryWrapper);

        //2.对dish_flavor表的插入
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }


    /**
     * 删除菜品信息和口味信息
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteWithFlavor(List<Long> ids) {

        ids.forEach(
                item -> {
                    LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
                    setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, item);
                    int count2 = setmealService.count(setmealLambdaQueryWrapper);
                    //查询套餐关联的，如果已经关联，抛出一个异常
                    if (count2 > 0) {
                        //抛出一个异常
                        throw new CustomException("已经关联套餐，不能删除");
                    }
                }
        );

        //删除dish表信息
        this.removeByIds(ids);


        ids.forEach(
                item -> {
                    //删除dish_flavor表里的信息
                    LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
                    queryWrapper.eq(DishFlavor::getDishId, item);
                    dishFlavorService.remove(queryWrapper);
                }
        );


    }
}
